/*
                       * Copyright 2010 Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.namuan.jp_stock.util;


import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class SessionStore {
	private static final String SESSION = "session";
    private static final String USERID = "userid";
    private static final String USERIMG = "userimg";
    private static final String AUTOLOGIN = "autologin";
    private static final String CHECKSTOP = "checkstop";
    private static final String KEY = "justplace-session";
   
    private static final String TRTOKEN = "tRequstToken";
    private static final String TRTOKENS = "tRequsttokenSecret";
    private static final String TATOKEN = "tAcessToken";
    
    public static boolean save(Justplace session, Context context) {
        Editor editor =
            context.getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
        editor.putString(SESSION, session.getmSession());
        editor.putString(USERID, session.getmUserNickname());
        editor.putString(USERIMG, session.getmProfilepic());
        editor.putBoolean(AUTOLOGIN, session.ismAutologin());
        editor.putBoolean(CHECKSTOP, session.ismCheckPopup());
        editor.putString(TRTOKEN, session.gettRequstToken());
        editor.putString(TRTOKENS, session.gettRequsttokenSecret());
        editor.putString(TATOKEN, session.gettAcessToken());
       return editor.commit();
    }

    public static boolean restore(Justplace  session, Context context) {
        SharedPreferences savedSession =
            context.getSharedPreferences(KEY, Context.MODE_PRIVATE);
        session.setmSession(savedSession.getString(SESSION, null));
        session.setmUserNickname(savedSession.getString(USERID, null));
        session.setmProfilepic(savedSession.getString(USERIMG, null));
        session.setmAutologin(savedSession.getBoolean(AUTOLOGIN,false));
        session.setmCheckPopup(savedSession.getBoolean(CHECKSTOP,false));
        session.settRequstToken(savedSession.getString(TRTOKEN, null));
        session.settRequsttokenSecret(savedSession.getString(TRTOKENS, null));
        session.settAcessToken(savedSession.getString(TATOKEN, null));
        return session.isSessionValid();
    }

    public static void clear(Context context) {
    	Editor editor = context.getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
		editor.putString(SESSION, null);
		editor.putString(USERID, null);
		editor.putString(USERIMG, null);
		editor.putString(AUTOLOGIN, null);

		editor.putString(TRTOKEN, null);
		editor.putString(TRTOKENS, null);
		editor.putString(TATOKEN, null);
		editor.commit();
    }
    public static void clearTwitter(Context context) {
        Editor editor = 
            context.getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
        editor.putString(TRTOKEN, null);
        editor.putString(TRTOKENS,null);
        editor.putString(TATOKEN, null);
        editor.commit();
    }
}
